home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Interapplication Communication / MenuScripter 4.0 / Sources / MSAEWindowUtils.c < prev    next >
Encoding:
Text File  |  1996-07-09  |  6.2 KB  |  348 lines  |  [TEXT/CWIE]

  1. // MSAEWindowUtils.c
  2. //
  3. // Original version by Jon Lansdell and Nigel Humphreys.
  4. // 4.0 and 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1996, all rights reserved.
  6.  
  7. #include "MSAEWindowUtils.h"
  8.  
  9. #include "MSWindow.h"        // for DPtrFromWindowPtr()
  10. #include "MSAEGetData.h"
  11. #include "MSAEUtils.h"
  12.  
  13. #include <LowMem.h>
  14.  
  15.     
  16. OSErr    GetDescOfNamedWindow( StringPtr nameStr, AEDesc* result )
  17. {
  18.     WindowToken        theToken;
  19.     OSErr            err = noErr;
  20.     
  21.     theToken.tokenWindow = GetNamedWindow( nameStr );
  22.     
  23.     if ( theToken.tokenWindow )
  24.         err = AECreateDesc( typeMyWndw, (Ptr)&theToken, sizeof( theToken ), result );
  25.     else
  26.         err = errAENoSuchObject;
  27.  
  28.     return(err);
  29. }
  30.  
  31.     
  32. OSErr    GetDescOfNamedDocument( StringPtr nameStr, AEDesc* result )
  33. {
  34.     WindowToken        theToken;
  35.     OSErr            err = noErr;
  36.     
  37.     theToken.tokenWindow = GetNamedDocument( nameStr );
  38.     
  39.     if ( theToken.tokenWindow )
  40.         err = AECreateDesc( typeMyDocument, (Ptr)&theToken, sizeof(theToken), result );
  41.     else
  42.         err = errAENoSuchObject;
  43.  
  44.     return(err);
  45. }
  46.  
  47.  
  48. short CountWindows(void)
  49. {
  50.     WindowPtr aWindow;
  51.     short     index;
  52.             
  53.     index = 0;
  54.     aWindow = (WindowPtr)LMGetWindowList();
  55.  
  56.     // iterate through windows
  57.     while ( aWindow )
  58.     {
  59.         if ( IsVisible( aWindow ) )
  60.             index++;                    
  61.  
  62.           aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow;
  63.     }
  64.     
  65.     return index;
  66. } // CountWindows
  67.  
  68.  
  69. OSErr    GetDescOfNthWindow(short index, AEDesc* result)
  70. {
  71.     WindowToken        theToken;
  72.     OSErr            err = noErr;
  73.     
  74.     theToken.tokenWindow = GetNthWindow( index );
  75.     
  76.     if (theToken.tokenWindow)
  77.         err = AECreateDesc(typeMyWndw, (Ptr)&theToken, sizeof(theToken), result);
  78.     else
  79.         err = errAEIllegalIndex;
  80.  
  81.     return(err);
  82. }
  83.  
  84.  
  85. OSErr    GetDescOfNthDocument( short index, AEDesc* result )
  86. {
  87.     WindowToken        theToken;
  88.     OSErr            err = noErr;
  89.     
  90.     theToken.tokenWindow = GetNthDocument( index );
  91.     
  92.     if (theToken.tokenWindow)
  93.         err = AECreateDesc( typeMyDocument, (Ptr)&theToken, sizeof(theToken), result );
  94.     else
  95.         err = errAEIllegalIndex;
  96.  
  97.     return(err);
  98. }
  99.  
  100.  
  101. WindowPtr    GetNthWindow( long index )
  102. {
  103.     long        result = 0;
  104.     WindowPtr    aWindow;
  105.     
  106.     aWindow = (WindowPtr)LMGetWindowList();
  107.     while (aWindow)
  108.     {
  109.         if ( IsVisible( aWindow ) )    // Only count visible windows
  110.             result++;
  111.         
  112.         if (result == index)
  113.             return(aWindow);
  114.         
  115.         aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow;
  116.     }
  117.     
  118.     return(NULL);
  119. }
  120.  
  121. WindowPtr    GetNthDocument( long index )
  122. {
  123.     long        result = 0;
  124.     WindowPtr    aWindow;
  125.     
  126.     aWindow = (WindowPtr)LMGetWindowList();
  127.     while (aWindow)
  128.     {
  129.         if ( IsDocumentWindow( aWindow ) && IsVisible( aWindow ) )
  130.             result++;
  131.         
  132.         if (result == index)
  133.             return(aWindow);
  134.         
  135.         aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow;
  136.     }
  137.     
  138.     return(NULL);
  139. }
  140.  
  141.  
  142. WindowPtr    GetNamedWindow( StringPtr theName )
  143. {
  144.     WindowPtr    aWindow;
  145.     Str255        title;
  146.     
  147.     aWindow = (WindowPtr)LMGetWindowList();
  148.     while (aWindow)
  149.     {
  150.         GetWTitle(aWindow, title);
  151.         
  152.         if (EqualString(theName, title, false, false))
  153.             return(aWindow);
  154.         
  155.         aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow;
  156.     }
  157.     
  158.     return(NULL);
  159. }
  160.  
  161. WindowPtr    GetNamedDocument( StringPtr theName )
  162. {
  163.     WindowPtr    aWindow;
  164.     Str255        title;
  165.     
  166.     aWindow = (WindowPtr)LMGetWindowList();
  167.     while (aWindow)
  168.     {
  169.         GetWTitle(aWindow, title);
  170.         
  171.         if ( IsDocumentWindow(aWindow)
  172.                 && EqualString(theName, title, false, false) )
  173.             return(aWindow);
  174.         
  175.         aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow;
  176.     }
  177.     
  178.     return(NULL);
  179. }
  180.  
  181.  
  182. long    GetWindowIndex( WindowPtr theWindow )
  183. {
  184.     long        result = 0;
  185.     WindowRef    aWindow;
  186.     
  187.     aWindow = (WindowPtr)LMGetWindowList();
  188.     while (aWindow)
  189.     {
  190.         if ( IsVisible( aWindow ) )
  191.             result++;
  192.         
  193.         if (theWindow == aWindow)
  194.             return(result);
  195.         
  196.         aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow;
  197.     }
  198.     
  199.     return(0);
  200. }
  201.  
  202. OSErr    SetWindowIndex( WindowPtr theWindow, long theIndex )
  203. {
  204.     long        maxIndex;
  205.     WindowPtr    behindWindow;
  206.     
  207.     maxIndex = CountWindows();
  208.     
  209.     if (theIndex < 0)                    // Handle negative indexes
  210.         theIndex = maxIndex + theIndex + 1;
  211.         
  212.     if (theIndex < 0 || theIndex > maxIndex)
  213.         return(errAEIllegalIndex);
  214.         
  215.     if (1 == theIndex)
  216.         SelectWindow( theWindow );
  217.     else
  218.     {
  219.         behindWindow = GetNthWindow( theIndex - 1 );
  220.         SendBehind( theWindow, behindWindow );
  221.     }
  222.     
  223.     return(noErr);
  224. }
  225.  
  226. long    GetDocumentIndex( WindowPtr theWindow )
  227. {
  228.     long        result = 0;
  229.     WindowRef    aWindow;
  230.     
  231.     aWindow = (WindowPtr)LMGetWindowList();
  232.     while (aWindow)
  233.     {
  234.         if ( IsDocumentWindow( aWindow ) && IsVisible( aWindow ) )
  235.             result++;
  236.         
  237.         if (theWindow == aWindow)
  238.             return(result);
  239.         
  240.         aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow;
  241.     }
  242.     
  243.     return(0);
  244. }
  245.  
  246. OSErr    SetDocumentIndex( WindowPtr theWindow, long theIndex )
  247. {
  248.     long        maxIndex;
  249.     WindowPtr    behindWindow;
  250.     
  251.     maxIndex = CountDocuments();
  252.     
  253.     if (theIndex < 0)                    // Handle negative indexes
  254.         theIndex = maxIndex + theIndex + 1;
  255.         
  256.     if (theIndex < 0 || theIndex > maxIndex)
  257.         return(errAEIllegalIndex);
  258.         
  259.     if (1 == theIndex && GetNthDocument(1) == GetNthWindow(1))
  260.         SelectWindow( theWindow );
  261.     else
  262.     {
  263.         behindWindow = GetNthDocument(theIndex - 1);
  264.         SendBehind(theWindow, behindWindow);
  265.     }
  266.     
  267.     return(noErr);
  268. }
  269.  
  270.  
  271.  
  272. Boolean    IsDocumentWindow( WindowPtr theWindow )
  273. {
  274.     DPtr        aDoc;
  275.     Boolean        result;
  276.  
  277.     if ( ! theWindow )
  278.         return false;
  279.         
  280.     aDoc = (DPtr)GetWRefCon( theWindow );
  281.     
  282.     result = ( kOrdinaryWind == aDoc->windowType || kResultsWind == aDoc->windowType );
  283.  
  284.     return true;
  285. }
  286.  
  287. Boolean    IsResultsWindow( WindowPtr theWindow )
  288. {
  289.     DPtr        aDoc;
  290.     Boolean        result;
  291.  
  292.     if ( ! theWindow )
  293.         return false;
  294.         
  295.     aDoc = (DPtr)GetWRefCon( theWindow );
  296.     
  297.     result = ( kResultsWind == aDoc->windowType );
  298.  
  299.     return result;
  300. }
  301.  
  302.  
  303. short    CountDocuments( void )
  304. {
  305.     WindowPtr aWindow;
  306.     short     index;
  307.             
  308.     index = 0;
  309.     aWindow = (WindowPtr)LMGetWindowList();
  310.  
  311.     // iterate through windows
  312.     while (aWindow)
  313.     {
  314.         if ( IsDocumentWindow( aWindow ) && IsVisible( aWindow ) )
  315.             index++;                    
  316.  
  317.         aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow;
  318.     }
  319.     
  320.     return index;
  321. } // CountWindows
  322.  
  323.  
  324. Boolean    IsVisible( WindowPtr theWindow )
  325. {
  326.     return ((WindowPeek)theWindow)->visible;
  327. }
  328.  
  329.  
  330. OSErr    GetWindowBounds( WindowPtr theWindow, Rect* theRect )
  331. {
  332.     AEDesc            aDesc = { typeNull, NULL };
  333.     WindowPropToken    aToken;
  334.     OSErr            anErr;
  335.     
  336.     aToken.tokenWindowToken.tokenWindow = theWindow;
  337.     aToken.tokenProperty = pBounds;
  338.     anErr = GetWindowTokenProperty( &aToken, &aDesc );
  339.     if ( noErr != anErr ) goto done;
  340.  
  341.     anErr = GetRectFromDescriptor( &aDesc, theRect );
  342.  
  343. done:
  344.     (void)AEDisposeDesc( &aDesc );
  345.     
  346.     return anErr;
  347. }
  348.